package org.crud4j.wicket.markup.html;

import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.crud4j.core.CrudBean;
import org.crud4j.core.CrudService;
import org.crud4j.wicket.CrudServiceInjectable;
import org.crud4j.wicket.markup.html.browse.BrowseModel;
import org.crud4j.wicket.markup.html.browse.BrowsePanel;

public class CrudPanel<T> extends Panel implements CrudServiceInjectable {

	private static final long serialVersionUID = 1L;

	private transient CrudService crudService;

	private Class<T> type;

	public CrudPanel(String id, IModel model) {
		super(id, model);

		this.type = (Class<T>) model.getObject();

		CrudBean crudBean = this.crudService.create(type);
		BrowseModel browseModel = new BrowseModel();
		browseModel.setDefinition(crudBean.getDef());
		Panel active = new BrowsePanel("active", new Model(browseModel));
		this.add(active);
	}

	public void setCrudService(CrudService crudService) {
		this.crudService = crudService;
	}

}
